<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="js/jquery.min.js?v=2.1.4"></script>
        <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>商品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" required="" lay-verify="name" autocomplete="off" class="layui-input"></div>
                        </div>
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>缩略图</label>
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn" id="uploadImg">
							  <i class="layui-icon">&#xe67c;</i>上传图片
							</button>
							<img alt="" id="img" src="" width="300px">
						</div>
                        </div>   
                    
                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>价格</label>
                        <div class="layui-input-inline">
                            <input type="text" id="price" name="price" required="" lay-verify="price" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">描述</label>
                        <div class="layui-input-block">
                            <textarea id="descrip" name="descrip" placeholder="请输入描述" class="layui-textarea" spellcheck="false"></textarea></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label">
                            	产品特色：</label>
                        <div class="layui-input-block">
                            <input type="text" id="tags" name="tags" lay-verify="tags" autocomplete="off" class="layui-input"></div>
                        <div class="layui-form-mid layui-word-aux">
                            	多个特色用逗号分隔</div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">服务保障</label>
                        <div class="layui-input-block">
                        	<input type="checkbox" name="service" value="1" lay-skin="primary" title="无购物">
							<input type="checkbox" name="service" value="2" lay-skin="primary" title="成团保障">
							<input type="checkbox" name="service" value="3" lay-skin="primary" title="安心游">
							<input type="checkbox" name="service" value="4" lay-skin="primary" title="无自费">
							                        	
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">
                            <span class="x-red">*</span>产品卖点</label>
                        <div class="layui-input-block">
                        	<input type="text" id="selling1" name="selling1"  lay-verify="" autocomplete="off" class="layui-input">
                        	<input type="text" id="selling2" name="selling2"  lay-verify="" autocomplete="off" class="layui-input">
                        	<input type="text" id="selling3" name="selling3"  lay-verify="" autocomplete="off" class="layui-input">
                        	                        	
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="L_pass" class="layui-form-label">
                            <span class="x-red">*</span>内容</label>
                        <div class="layui-input-block">
                            <textarea id="content" name="content" placeholder="请输入内容" class="layui-textarea" spellcheck="false"></textarea></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="modify" lay-submit="">修改</button></div>
                </form>
            </div>
        </div>
        <input id="id" type="hidden">
        <script>

        layui.use(['form', 'layer','jquery'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;
                //自定义验证规则
                form.verify({
                	name: function(value) {
                		if (value.length==0) {
                            return '请输入商品名称！';
                        }
                        if (value.length > 15) {
                            return '商品名称超过15个字符';
                        }
                    },
                    price: function(value) {
                    	if (value.length==0) {
                            return '请输入价格';
                        }
                        if (value.length > 6) {
                            return '价格不能超过10万';
                        }
                    },
                });

                //监听提交
                form.on('submit(modify)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给php
                    doSubmit();
                    
                    return false;
                });

            });
        layui.use('upload', function(){
        	  var upload = layui.upload;
        	   
        	  //执行实例
        	  var uploadInst = upload.render({
        	    elem: '#uploadImg' //绑定元素
        	    ,url: 'goods/upload' //上传接口
        	    ,field:"image"
        	    ,done: function(res){
        	      //上传完毕回调
        	    	if(res.code==0){
                        $("#img").attr('src',res.data.imageURL);
                        imageURL=res.data.dbURL;
                    }else{
                        //alert(res.msg);
                        layer.msg(res.msg);
                    }
        	    }
        	    ,error: function(){
        	      //请求异常回调
        	    }
        	  });
        	});</script>
<script type="text/javascript">

var imageURL="";
function doSubmit(){
	layer.confirm('您确定要修改吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			modify();
		}, function(){
		  
		});
}
function modify(){
	var id=$("#id").val();
	var price=$("#price").val();
	var name=$("#name").val();
	var descrip=$("#descrip").val();
	var tags=$("#tags").val();
	
	
	var services ="";
	$('input[name="service"]:checked').each(function(){
		 //service.push($(this).val());
		 services+=$(this).val()+",";
	});
	services = services.substr(0, services.length - 1);
	
	var selling1=$("#selling1").val();
	var selling2=$("#selling2").val();
	var selling3=$("#selling3").val();
	var content=$("#content").val();
	
	if (name==""){
		$("#name").focus();
		$("#name_msg").html("请输入名称！")
		return
	}else{
		$("#username_msg").html("")
	}
	
	$.ajax({   
           url:'goods/updateById',   
           type:'post',   
           dataType:'json',//服务器端返回的数据格式是json
		   data: {
			   "id":id,
			   "price":price,
			   "name":name,
			   "descrip":descrip,
			   "tags":tags,
			   "services":services,
			   "selling1":selling1,
			   "selling2":selling2,
			   "selling3":selling3,
			   "content":content,
			   "img":imageURL,
		   },//发给服务器端的数据
           success:function(res){   //data:服务器端返回给浏览器端的数据
		   		if(res.code==0){
		   			layer.alert(res.msg, {
                        icon: 6
                    },
                    function() {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新 
                        xadmin.father_reload();
                    });
		   		}else{
		   			alert(res.msg);
		   		}
           },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
        	   		alert(XMLHttpRequest);
        	   		alert(textStatus);
        	   		alert(errorThrown);
           }
           
       });
}

function getParam(variable){
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
    }
    return(false);
}
function init(){
	var id=getParam("id");
	$.ajax({
     url:'goods/showGoodsById', /*接口域名地址*/
     type:'get',
     data: {
     	"id":id
     },
     success:function(res){
         //console.log(res.data);
         if(res.code==0){
         	$("#id").val(res.data.id);
         	$("#price").val(res.data.price);
             $("#name").val(res.data.name);
             $("#img").attr('src',"../"+res.data.img);
             imageURL=res.data.img;
             $("#descrip").val(res.data.descrip);
             $("#tags").val(res.data.tags);
             $("#selling1").val(res.data.selling1);
             $("#selling2").val(res.data.selling2);
             $("#selling3").val(res.data.selling3);
             $("#content").val(res.data.content);
             var services=res.data.services;
             if(services != null && services.trim().length > 0){
             	servicesArray=services.split(",")
             	console.log(servicesArray)
             	
             	for(var i=0;i<servicesArray.length;i++){
             		$('input[name="service"]').each(function(){
             			 //service.push($(this).val());
             			 services+=$(this).val()+",";

                  		if(servicesArray[i]==$(this).val()){
                  			$(this).prop('checked',true);
                  		}
             		});
             	}
             }
         }else{
             layer.alert(res.msg, {icon: 0,title: '提示'})
         }
     },
     error:function (XMLHttpRequest, textStatus, errorThrown) {
	   		alert(XMLHttpRequest);
	   		alert(textStatus);
	   		alert(errorThrown);
		}
 })
}
init()
</script>
    </body>

</html>